package com.it2297.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.it2297.controller.ProductController;
import com.it2297.main.UserApp;
import com.it2297.model.Comment;
import com.it2297.model.Product;
import com.it2297.model.User;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

public class CreateCommentDialog extends JDialog {
	private final ProductController pc = new ProductController(this);
	private ProductDetailDialog p;
	private final JPanel contentPanel = new JPanel();
	private final JTextField titleTxt = new JTextField();
	private final JTextArea msgtxt = new JTextArea();

	public CreateCommentDialog() {
		titleTxt.setEditable(false);
		titleTxt.setBounds(90, 11, 164, 48);
		titleTxt.setColumns(10);
		setBounds(100, 100, 450, 300);
		this.setModalityType(getModalityType().APPLICATION_MODAL);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		contentPanel.add(titleTxt);
		msgtxt.setBounds(10, 83, 424, 135);

		contentPanel.add(msgtxt);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton submitButton = new JButton("Submit");
				submitButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						try {
							createComment();
						} catch (Exception ex) {
							ex.printStackTrace();
						} finally {
							cancel();
						}
					}
				});

				buttonPane.add(submitButton);
				getRootPane().setDefaultButton(submitButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						cancel();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	public void setTitle(String title) {
		titleTxt.setText(title);
	}

	public String getUserid() {
		return UserApp.getCurrentUser().getNric();
	}

	public String getTitle() {
		return titleTxt.getText();
	}

	public String getComment() {
		return msgtxt.getText();
	}

	public void createComment() {
		pc.CreateComment();
	}

	public void cancel() {
		this.dispose();
	}
}
